package com.face.face.task;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.face.face.activity.BaseActivity;
import com.face.face.entity.Pojo;
import com.face.face.utils.ServiceUtil;

public class RegisterTask extends AsyncTask<Void, Integer, Boolean> {

    private Context context = null;
    private Handler handler = null;
    private Pojo pojo = null;
    private String uid;
    private String name;

    public RegisterTask(Context context, Handler handler, String name, String uid) {
        this.context = context;
        this.handler = handler;
        this.name = name;
        this.uid = uid;
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        try {
            pojo = ServiceUtil.getInstance().register(name, uid);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        if (handler != null) {
            Message message = new Message();
            if (result) {
                message.what = BaseActivity.SUCCESS;
                message.obj = pojo;
            } else {
                Toast.makeText(context, "请求失败", Toast.LENGTH_SHORT).show();
                message.what = BaseActivity.FAILED;
            }
            handler.handleMessage(message);
        }
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
        if (handler != null) {
            Message message = new Message();
            message.what = BaseActivity.FAILED;
            handler.handleMessage(message);
        }
    }
}